/**
 * 2017年3月9日
 */
package cn.edu.bjtu.alex.rewrite.components;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;


import com.google.common.collect.Sets;

import cn.edu.bjtu.alex.rewrite.Context;
import cn.edu.bjtu.alex.rewrite.interfaces.Term;
import cn.edu.bjtu.alex.rewrite.interfaces.TermFeatureable;
import cn.edu.bjtu.alex.rewrite.interfaces.impl.FeaturedTerm;
import cn.edu.bjtu.alex.rewrite.tools.FileUtils;

/**
 * @author Alex
 *
 */
public class IDFModelTransformer extends BaseComponent{

	/**
	 * @param ctx
	 */
	public IDFModelTransformer(Context ctx) {
		super(ctx);
	}
	@Override
	protected void fitInternal() {}

	protected void transformInternal() {
		//如果IDF值没导入,就导入它
		if(ctx.featuredTerms().size()==0){
			File dict = new File(ctx.getOutputDir(),ctx.getFeatureTermVectorFileName());
			FileInputStream fis = null;
			BufferedReader reader = null;
			try {
				fis = new FileInputStream(dict);
				reader = new BufferedReader(new InputStreamReader(fis, ctx.getCharset()));
				String line = null;
				Set<TermFeatureable> terms = Sets.newHashSet();
				while((line = reader.readLine()) != null) {
					line = line.trim();
					if(!line.isEmpty()) {
						String[] aWord = line.split("\\s+");
						if(aWord.length == 3) {
							String word = aWord[0];
							int wordId = Integer.parseInt(aWord[1]);
							FeaturedTerm term = new FeaturedTerm(word);
							
							term.setId(wordId);
							terms.add(term);
							term.setMeasureValue(Double.parseDouble(aWord[2]));
							
							LOG.info("Load CHI term: word=" + word + ", wordId=" + wordId);
						}else{
							throw new RuntimeException("去找邓罗丹把程序改下,在feature.txt第三列输出词的idf值");
						}
					}
				}
				ctx.setFeaturedTerms(terms);
				
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				FileUtils.closeQuietly(fis, reader);
			}		
		}
		//
		Map<String,Map<String, Term>> segmentedDocs = ctx.getTransformedDocument();
		for(Entry<String,Map<String,Term>> entry:segmentedDocs.entrySet()){
			Map<String,Term> words = entry.getValue();
			for(Entry<String,Term> t:words.entrySet()){
				Term term = t.getValue();
				
				if(ctx.featuredTermsMap().containsKey(term.getWord())){
					double tf = term.getTf();
					term.setTfidf(tf * ctx.featuredTermsMap().get(term.getWord()).getMeasureValue());
				}
				
			}
		}
		
		System.out.println("end");
		
		
	}

}
